/*
 * 描述
 * Created by: litangyu
 * 包含类名列表
 * 版本信息，版本号 v1.0
 * 创建日期 16/8/30
 * 版权声明
*/
package com.foton.logistic.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.foton.logistic.R;
import com.foton.logistic.listener.IOnAbnormalListener;
import com.foton.logistic.model.order.AbnormalEntity;
import com.foton.logistic.util.OptionUtil;
import com.foton.logistic.util.TimeUtil;
import com.foton.logistic.view.MyGridView;
import com.foton.logistic.view.ultimate.UltimateViewAdapter;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 描述
 * Created by: litangyu<br/>
 * Created on: 16/8/30 下午3:55
 */
public class AbnormalAdapter extends UltimateViewAdapter {

    private String Image_prefix;
    private Context mContext;
    private List<AbnormalEntity> list;
    private int imageHeight;
    private IOnAbnormalListener iOnAbnormalListener;

    public AbnormalAdapter(Context context, List<AbnormalEntity> list,int imageHeight,String image_prefix) {
        this.mContext = context;
        this.list = list;
        this.imageHeight = imageHeight;
        this.Image_prefix = image_prefix;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
        View view;
        MyViewHolder viewHolder;
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout
                .LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ft_adapter_abnormal,
                null);
        view.setLayoutParams(layoutParams);
        viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public int getAdapterItemCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (position < getItemCount() && (customHeaderView != null ? position <= list.size() :
                position < list.size()) && (customHeaderView != null ? position > 0 : true)) {
            final MyViewHolder viewHolder = (MyViewHolder) holder;
            AbnormalEntity abnormalEntity = list.get(position);
            viewHolder.abnormalTime.setText(TimeUtil.formatGLWZTime(abnormalEntity.operateTime));
            viewHolder.abnormalMessage.setText(abnormalEntity.content);
            if (abnormalEntity.isExpanded){
                viewHolder.abnormalArrow.setSelected(true);
                viewHolder.abnormalContentLayout.setVisibility(View.VISIBLE);
            }else {
                viewHolder.abnormalArrow.setSelected(false);
                viewHolder.abnormalContentLayout.setVisibility(View.GONE);
            }
            ImageGridAdapter adapter = new ImageGridAdapter(mContext, OptionUtil.formatImagesString(abnormalEntity.imageList,Image_prefix),imageHeight);
            viewHolder.abnormalPictures.getLayoutParams().height = imageHeight;
            viewHolder.abnormalPictures.setAdapter(adapter);
            viewHolder.abnormalExpandLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iOnAbnormalListener!=null){
                        iOnAbnormalListener.onExpand(position);
                    }
                }
            });
            viewHolder.abnormalPictures.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int index, long id) {
                    if (iOnAbnormalListener!=null){
                        iOnAbnormalListener.onImagePreview(position, index);
                    }
                }
            });
        }
    }

    public void setImage_prefix(String image_prefix) {
        this.Image_prefix = image_prefix;
    }

    public void setiOnAbnormalListener(IOnAbnormalListener iOnAbnormalListener) {
        this.iOnAbnormalListener = iOnAbnormalListener;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.ft_adapter_abnormal_time)
        TextView abnormalTime;
        @InjectView(R.id.ft_adapter_abnormal_arrow)
        ImageView abnormalArrow;
        @InjectView(R.id.ft_adapter_abnormal_expand_layout)
        LinearLayout abnormalExpandLayout;
        @InjectView(R.id.ft_adapter_abnormal_message)
        TextView abnormalMessage;
        @InjectView(R.id.ft_adapter_abnormal_pictures)
        MyGridView abnormalPictures;
        @InjectView(R.id.ft_adapter_abnormal_content_layout)
        LinearLayout abnormalContentLayout;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }

}
